<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="指定商品">
          <a-select
            showSearch
            :defaultActiveFirstOption="false"
            :showArrow="false"
            :filterOption="false"
            placeholder="指定商品"
            @search="handleSearch"
            notFoundContent="null"
            v-decorator="['productSpuId', { rules: [{ required: false, message: '请指定商品' }] }]"
          >
            <a-select-option value="10">正在加载</a-select-option>
            <a-select-option v-for="d in spuList" :key="d.id">{{ d.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="商品推荐类型">
          <a-radio-group v-decorator="['type', validatorRules.status]">
            <a-radio-button value="1">热卖推荐</a-radio-button>
<!--            <a-radio-button value="2">新品推荐</a-radio-button>-->
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-radio-group v-decorator="['status', validatorRules.status]">
            <a-radio-button value="0">开启</a-radio-button>
            <a-radio-button value="1">关闭</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
          <a-input-number v-decorator="['sort', validatorRules.sort]" />
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
          <a-input placeholder="请输入备注" v-decorator="['memo', validatorRules.memo]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'

export default {
  name: 'PrdRecomModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      spuList: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        deleted: { rules: [{ required: true, message: '请输入是否删除!' }] },
        memo: { rules: [{ required: true, message: '请输入备注!' }] },
        productSpuId: { rules: [{ required: true, message: '请输入商品 Spu 编号!' }] },
        sort: { rules: [{ required: true, message: '请输入排序!' }] },
        status: { rules: [{ required: true, message: '请输入状态!' }] },
        type: { rules: [{ required: true, message: '请输入类型!' }] }
      },
      url: {
        add: '/admins/product_recommend/add',
        edit: '/admins/product_recommend/update'
      }
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'deleted', 'memo', 'productSpuId', 'sort', 'status', 'type'))
        //时间格式化
      })
      if (this.model.productSpuId != undefined) {
        let params = { id: this.model.productSpuId }
        this.getSpuList(params)
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleSearch(value) {
      let params = { name: value + '*' }
      this.getSpuList(params)
    },

    getSpuList(params) {
      getAction('/goodsMain/web/list', params).then(res => {
        if (res.success) {
          this.spuList = res.result.records
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
      })
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          //时间格式化
          formData.productSpuName = that.getGoodsname(formData.productSpuId)
          console.log(formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    getGoodsname(spuId) {
      let list = this.spuList
      for (let i in list) {
        if (list[i].id == spuId) {
          return list[i].goodsName
        }
      }
      return ''
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style lang="less" scoped></style>
